﻿package com.hh.bss.util.extjs;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.DefaultValueProcessor;
import net.sf.json.util.CycleDetectionStrategy;


public class JsonHelper {
	
//	public static HttpServletResponse getResponse() {
//		return ServletActionContext.getResponse();
//	}
//	
//	public static HttpServletRequest getRequest() {
//		return ServletActionContext.getRequest();
//	}
	
	public static void outJsonString(HttpServletResponse response,String str) {
		response.setContentType("text/javascript;charset=UTF-8");
		outString(response,str);
	}

 
	  /**   
     * JSON 时间解析器具 
     *    
     * @param datePattern   
     * @return   
     */   
    public static JsonConfig configJson(String datePattern) {    
        JsonConfig jsonConfig = new JsonConfig();    
        jsonConfig.setExcludes(new String[] { "" });    
        jsonConfig.setIgnoreDefaultExcludes(false);    
        jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);    
        jsonConfig.registerJsonValueProcessor(Date.class,    
                new JsonDateValueProcessor(datePattern));    
   
        jsonConfig.registerJsonValueProcessor(java.util.Date.class,    
                new JsonDateValueProcessor(datePattern));
        jsonConfig.registerJsonValueProcessor(java.sql.Timestamp.class,    
                new JsonDateValueProcessor(datePattern));
        
        //处理空的Integer
        jsonConfig.registerDefaultValueProcessor(Integer.class,
				new DefaultValueProcessor() {
					public Object getDefaultValue(Class type) {
						return null;
					}
				});
      //处理空的Double
        jsonConfig.registerDefaultValueProcessor(Double.class,
				new DefaultValueProcessor() {
					public Object getDefaultValue(Class type) {
						return null;
					}
				});
        
        return jsonConfig;    
    }    

	public static void outJson(HttpServletResponse response,Object obj) {
        JsonConfig jsonConfig = configJson("yyyy-MM-dd HH:mm:ss");    
        jsonConfig.setExcludes(new String[]{"handler","hibernateLazyInitializer"});        
		outJsonString(response,JSONObject.fromObject(obj,jsonConfig).toString());
	}

	public static void outJsonArray(HttpServletResponse response,Object array) {
		JsonConfig jsonConfig = configJson("yyyy-MM-dd HH:mm:ss");    
        jsonConfig.setExcludes(new String[]{"handler","hibernateLazyInitializer"});
		//outJsonString(response,JSONArray.fromObject(array).toString());
		outJsonString(response,JSONArray.fromObject(array,jsonConfig).toString());
	}
	
	public static void outString(HttpServletResponse response,String str) {
		try {
			PrintWriter out = response.getWriter();
			out.write(str);
		} catch (IOException e) {
		}
	}

	public static void outXMLString(HttpServletResponse response,String xmlStr) {
		response.setContentType("application/xml;charset=UTF-8");
		outString(response,xmlStr);
	}
	
}
